package com.nuoniu.sibanyun.controller.erp;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.erp.ErpProjectClass;
import com.nuoniu.sibanyun.service.erp.IErpProjectClassService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 项目分类 前端控制器
 * </p>
 *
 * @author baiqiang
 * @since 2020-10-12
 */
@RestController
@RequestMapping("/erp/projectClass")
@Api(tags = "项目分类管理")
public class ErpProjectClassController extends BaseController {

    @Autowired
    private IErpProjectClassService projectClassService;

    @GetMapping("pageList")
    @ApiOperation("获取列表")
    public HttpResult pageList(Page page, ErpProjectClass projectClass) {
        Page<ErpProjectClass> projectClassPage = projectClassService.pageList(page, projectClass);
        return HttpResult.ok(projectClassPage);
    }

    @PostMapping("save")
    @ApiOperation("保存")
    public HttpResult save(@RequestBody ErpProjectClass projectClass) {
        if(StringUtils.isBlank(projectClass.getCode())) {
            throw new NuoNiuException("编码不能为空");
        }
        if(StringUtils.isBlank(projectClass.getName())) {
            throw new NuoNiuException("名称不能为空");
        }
        //校验 编码


        UserInfo userInfo = UserThreadLocal.get();
        if(projectClass.getId() != null) {
            projectClass.setUpdatedDate(LocalDateTime.now());
            projectClass.setUpdatedUserId(userInfo.getUserId());
        }else {
            projectClass.setCreatedUserId(userInfo.getUserId());
        }
        projectClassService.saveOrUpdate(projectClass);
        return HttpResult.ok();
    }

    @PostMapping("delete")
    @ApiOperation("删除")
    public HttpResult delete(@RequestBody List<Long> ids) {
        for(Long id : ids) {
            projectClassService.removeById(id);
        }
        return HttpResult.ok();
    }

}
